package com.ruoyi.basic.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import com.ruoyi.basic.domain.TbPriceHistory;
import com.ruoyi.basic.mapper.TbPriceHistoryMapper;
import com.ruoyi.basic.service.ITbPriceHistoryService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;

/**
 * 工人工日单价历史数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-05
 */
@Service
public class TbPriceHistoryServiceImpl implements ITbPriceHistoryService
{
    @Autowired
    private TbPriceHistoryMapper tbPriceHistoryMapper;

    private static final Logger log = LoggerFactory.getLogger(TbPriceHistoryServiceImpl.class);

    /**
     * 查询工人工日单价历史数据
     * 
     * @param id 工人工日单价历史数据主键
     * @return 工人工日单价历史数据
     */
    @Override
    public TbPriceHistory selectTbPriceHistoryById(Long id)
    {
        return tbPriceHistoryMapper.selectTbPriceHistoryById(id);
    }

    /**
     * 查询工人工日单价历史数据列表
     * 
     * @param tbPriceHistory 工人工日单价历史数据
     * @return 工人工日单价历史数据
     */
    @Override
    public List<TbPriceHistory> selectTbPriceHistoryList(TbPriceHistory tbPriceHistory)
    {
        return tbPriceHistoryMapper.selectTbPriceHistoryList(tbPriceHistory);
    }

    /**
     * 新增工人工日单价历史数据
     * 
     * @param tbPriceHistory 工人工日单价历史数据
     * @return 结果
     */
    @Override
    public int insertTbPriceHistory(TbPriceHistory tbPriceHistory)
    {
        tbPriceHistory.setCreateTime(DateUtils.getNowDate());
        tbPriceHistory.setUpdateTime(DateUtils.getNowDate());
        return tbPriceHistoryMapper.insertTbPriceHistory(tbPriceHistory);
    }

    /**
     * 修改工人工日单价历史数据
     * 
     * @param tbPriceHistory 工人工日单价历史数据
     * @return 结果
     */
    @Override
    public int updateTbPriceHistory(TbPriceHistory tbPriceHistory)
    {
        tbPriceHistory.setUpdateTime(DateUtils.getNowDate());
        return tbPriceHistoryMapper.updateTbPriceHistory(tbPriceHistory);
    }

    /**
     * 批量删除工人工日单价历史数据
     * 
     * @param ids 需要删除的工人工日单价历史数据主键
     * @return 结果
     */
    @Override
    public int deleteTbPriceHistoryByIds(String ids)
    {
        return tbPriceHistoryMapper.deleteTbPriceHistoryByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工人工日单价历史数据信息
     * 
     * @param id 工人工日单价历史数据主键
     * @return 结果
     */
    @Override
    public int deleteTbPriceHistoryById(Long id)
    {
        return tbPriceHistoryMapper.deleteTbPriceHistoryById(id);
    }

    @Override
    public String importData(List<TbPriceHistory> list, boolean updateSupport, String userName) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TbPriceHistory tbPriceHistory : list)
        {
            try
            {
                // 主要数据不能为空
                if (StringUtils.isNull(tbPriceHistory.getYear()) ||
                        StringUtils.isNull(tbPriceHistory.getPriceType()) ||
                        StringUtils.isNull(tbPriceHistory.getCountryType())) {
                    continue;
                }
                // 验证是否存在
                List<TbPriceHistory> checkList = tbPriceHistoryMapper.selectTbPriceHistoryList(tbPriceHistory);
                if (checkList.isEmpty())
                {
                    tbPriceHistory.setCreateBy(userName);
                    tbPriceHistory.setCreateTime(DateUtils.getNowDate());
                    tbPriceHistory.setUpdateBy(userName);
                    tbPriceHistory.setUpdateTime(DateUtils.getNowDate());
                    this.insertTbPriceHistory(tbPriceHistory);
                    successNum++;
                    //successMsg.append("<br/>第" + successNum + "条数据导入成功");
                }
                else if (updateSupport)
                {
                    tbPriceHistory.setUpdateBy(userName);
                    tbPriceHistory.setId(checkList.get(0).getId());
                    this.updateTbPriceHistory(tbPriceHistory);
                    successNum++;
                    //successMsg.append("<br/>第" + successNum + "条数据更新成功");
                }
                else
                {
                    failureNum++;
                    //failureMsg.append("<br/>第" + failureNum + "条数据已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>第" + failureNum + "条数据导入失败：";
                //failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }
}
